package com.tedu.parkinglot.payRecord.controller;

import com.alibaba.excel.EasyExcel;
import com.tedu.parkinglot.base.response.JsonResult;
import com.tedu.parkinglot.parkRecord.pojo.dto.ParkRecordQuery;
import com.tedu.parkinglot.parkRecord.pojo.vo.ParkRecordVO;
import com.tedu.parkinglot.payRecord.pojo.dto.PayRecordQuery;
import com.tedu.parkinglot.payRecord.pojo.vo.ParkChargePayRecordVO;
import com.tedu.parkinglot.payRecord.pojo.vo.UserCardPayRecordVO;
import com.tedu.parkinglot.payRecord.service.PayRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/payRecord/")
public class PayRecordController {
    @Autowired
    private PayRecordService payRecordService;

    List<UserCardPayRecordVO> userCardPayRecordVOS = new ArrayList<>();
    @GetMapping("searchParkCharg")
    public JsonResult searchParkCharg(PayRecordQuery payRecordQuery){
        log.debug("payRecordQuery:{}",payRecordQuery);
        List<ParkChargePayRecordVO> payRecords= payRecordService.searchParkChargRecord(payRecordQuery);
        log.debug("payRecords:{}",payRecords);
        return JsonResult.ok(payRecords);
    }
    @GetMapping("searchParkCard")
    public JsonResult searchParkCard(PayRecordQuery payRecordQuery){
        log.debug("searchParkCard payRecordQuery:{}",payRecordQuery);
        userCardPayRecordVOS = payRecordService.searchUserCardRecord(payRecordQuery);
        log.debug("userCardPayRecordVOS:{}",userCardPayRecordVOS);
        return JsonResult.ok(userCardPayRecordVOS);
    }
    @GetMapping("download")
    public void download(HttpServletResponse response, PayRecordQuery payRecordQuery) throws IOException {
        userCardPayRecordVOS = payRecordService.searchUserCardRecord(payRecordQuery);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("车位卡购买记录", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), UserCardPayRecordVO.class).sheet("模板").doWrite(userCardPayRecordVOS);
    }


}
